// pages/restt/restt.js
const util = require('../../utils/util.js')
const app = getApp();
const cloudUrl = app.globalData.cloudUrl;
const tempUrl = cloudUrl+"shop-menu-icon11.png";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cloudUrl:app.globalData.cloudUrl,
    //幻灯片
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    background: [{"id":1, "img":cloudUrl+"restt/swiper1.png"},
                {"id":1, "img":cloudUrl+"restt/swiper2.png"}],
    infoDescFlag:true,
    infoDesc:"顺利地考上了看附件是浪费时间开发商考虑士大夫顺利地考上了看附件是浪费时间开发商考虑士大夫",
    infoDescTmp:"",
    allMenus1:[
      {id:1,name:"商家",txtColor:"#000000",select:true, border: "2px solid rgba(65, 89, 224, 0.562)"},
      {id:2,name:"点餐",txtColor:"#000000",select:false},
      {id:3,name:"评价",txtColor:"#000000",select:false}
    ],
    tmpData1:{img:tempUrl,name:"松阴溪绿道北岸 150",showLine:true},
    dcList:[
      {id:"t1",id2:"s1",index:0,type:"果饮奶茶",txtColor:"#626262",select:false,list:[{name:"四季奶青",sell:0,price:18},{name:"抹茶厚椰乳冰",sell:0,price:18}]},
      {id:"t2",index:1,type:"咖啡系列",list:[{name:"拿铁/冰",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t3",index:2,type:"茶水茶点",list:[{name:"土红茶-壶",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t4",index:3,type:"茶水茶点1",list:[{name:"土红茶-壶1",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t5",index:4,type:"茶水茶点2",list:[{name:"土红茶-壶2",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t6",index:5,type:"茶水茶点3",list:[{name:"土红茶-壶3",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t7",index:6,type:"松阳味道",list:[{name:"松阳沙擂",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t8",index:7,type:"松阳味道",list:[{name:"松阳沙擂",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t9",index:8,type:"松阳味道",list:[{name:"松阳沙擂",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
      {id:"t10",index:9,type:"松阳味道",list:[{name:"松阳沙擂",sell:0,price:18},{name:"松阳沙擂",sell:0,price:18}]},
    ],
    scrollId:"s1",
    testId:"t1",
    typeIndex:0,
    dcInit:false,
    dcCurIndex:0,
    dcTop:0,
    enabled:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //this.setData({infoDescTmp:this.data.infoDesc});
    this.onInfoArrowClick(null);

    const dcList = this.data.dcList;
    for(var i=0; i<dcList.length; i++){
      dcList[i]["id2"] = "s" + (i+1);
    }
    this.setData({dcList:dcList});
  },
  onAllMenu1Click1(e){
    this.onAllMenu1Click(0);
  },
  onAllMenu1Click2(e){
    this.onAllMenu1Click(1);
    this.initDcListPos();
  },
  onAllMenu1Click3(e){
    this.onAllMenu1Click(2);
  },
  //类型：商家、点餐、评价
  onAllMenu1Click(index=0){
    if(!index){
      index = 0;
    }
    var idx = index;
    this.data.allCurMenu1Index = idx;
    const menus = this.data.allMenus1;
    var menu = null;
    for(var i=0; i<menus.length; i++){
      menu = menus[i];
      if(idx == i){
        menu.select = true;
        menu.border = "2px solid rgba(65, 89, 224, 0.562)";
      }
      else{
        menu.select = false;
        menu.border = "";
      }
    }
    this.data.typeIndex = idx;
    this.setData({allMenus1:this.data.allMenus1});
  },
  //点餐-左侧tab
  onMenuClick(e, index=-1, setId = true){
    var idx = 0;
    if(index >= 0){
      idx = index;
    }
    else{
      idx = e.currentTarget.dataset.index;
    }
    const menus = this.data.dcList;
    var menu = null;
    for(var i=0; i<menus.length; i++){
      menu = menus[i];
      if(idx == i){
        menu.txtColor = "rgb(51,153,255)";
        menu.left = "35rpx";
        menu.bgcolor = "white";
        this.data.testId = this.data.dcList[i].id;
        this.data.scrollId = this.data.dcList[i].id2;
      }
      else{
        menu.txtColor = "rgb(82,82,82)";
        menu.left = "20rpx";
        menu.bgcolor = "rgb(237, 237, 237)";
      }
      menu.select = !(idx == i);
    }
    this.data.dcCurIndex = idx;
    console.log("---" + this.data.scrollId);
    if(setId){
      this.setData({dcList:this.data.dcList,testId:this.data.testId,scrollId:this.data.scrollId});
    }
    else{
      //console.log("---" + this.data.scrollId);
      this.setData({dcList:this.data.dcList,scrollId:this.data.scrollId});
    }
  },

  onScrollHandler1(e){

  },
  onScrollEnd(e){
    console.log('---------scroll end---------');
    console.log(e);
  },
  onScrollHandler(e){
    //console.log(e);
    this.initDcListPos();
    const top = e.detail.scrollTop;
    const menus = this.data.dcList;
    var scrollId = 0;
    for(var i=0; i<menus.length; i++){
      if(top >= menus[i].posy){
        scrollId = i;
      }
    }
    console.log("scroId = " + scrollId);
    if(this.data.dcCurIndex != scrollId){
      this.onMenuClick(null, scrollId, false);
    }
  },
  initDcListPos(){
    if(!this.data.dcInit){
      this.setData({dcInit:true});
      const menus = this.data.dcList;
      var firstTop = 0;
      for(var i=0; i<menus.length; i++){
        const query = wx.createSelectorQuery();
        query.select('#t'+(i+1)).boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec(function(res){
          const idx = res[0].dataset.index;
          if(firstTop == 0){
            firstTop = res[0].top;
          }
          menus[idx].posy = (res[0].top - firstTop);
          //console.log(res);
        })
      }

      //获取并设置剩余高度
      const _this = this;
      const query = wx.createSelectorQuery();
      query.select('#dc').boundingClientRect();
      //query.selectViewport().scrollOffset();
      query.exec(function(res){
        _this.setData({dcTop:res[0].top});
        console.log(res);
      })
      //console.log("firstTop="+this.data.dcTop);
      this.setData({dcList:this.data.dcList});
    }
  },
  onInfoArrowClick(e){
    var flag = !this.data.infoDescFlag;
    var str = this.data.infoDesc;
    if(!flag){
      //console.log("str.len="+str.length);
      if(str.length > 23){
        str = str.substring(0, 21) + "...";
      }
      //console.log(encodeURI(str));
    }
    this.setData({
      infoDescFlag:flag,
      infoDescTmp:str
    });
    if(e && this.data.typeIndex == 1){
      this.setData({dcInit:false});
      this.initDcListPos();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})